package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.refundStorage.RefundStorageListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

public class RefundStorageManageActivity extends BaseActivity {

    TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView refundStorageList;

    private List<RefundStorageListBean.DataBean.PickingListBean> allData;

    private RefundStorageListAdapter adapter;

    private String mCondition;

    private boolean mFlag = false;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_manage);
        initView(RefundStorageManageActivity.this);
        initData(RefundStorageManageActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refundStorageList = (PullableListView) activity.findViewById(R.id.refundStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.refundStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_return_storage_manage_title));
        ActivityUtils.initBackAndFunction(activity, "主页", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        refundStorageList.setPullUpFlag(false);
        refundStorageList.setPullDownFlag(false);
        getExtra(activity);
        if (mFlag) {
            //后续功能跳转
//            Intent intent = new Intent(activity, .class);
//            startActivity(intent);
//            finish();
        } else {
            mCondition = "";
            allData = new ArrayList<>();
            addRefundListData(activity, allData, refundStorageList, true);
        }
    }

    private void addRefundListData(Context context, List<RefundStorageListBean.DataBean.PickingListBean> saleOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new RefundStorageListAdapter(context, saleOutStorageList, mCondition, (String condition) -> {
                String checkResult = check(condition);
                if (checkResult == null) {
                    mCondition = condition;
                    getData(context, mCondition);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String saleNumber) {
        String result = null;
        if (TextUtils.isEmpty(saleNumber)) {
            result = "查询条件不可以为空";
        }
        return result;
    }

    private void getData(Context context, String condition) {
        RefundStorageListRequest.request(context, condition, new GsonBeanListener<RefundStorageListBean>() {
            @Override
            public void onSuccess(RefundStorageListBean bean) {
                allData = bean.getData().getPickingList();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, "未查到数据");
                }
                addRefundListData(context, ActivityUtils.getShowData(allData, 0), refundStorageList, true);
            }
        });
    }

    public interface RefundStorageManageListener {
        void onNoDoubleClickListener(String condition);
    }

    private void getExtra(Activity activity) {
        mFlag = activity.getIntent().getBooleanExtra(OutStorageExtra.FLAG, false);
    }

}
